import { getLogin } from '@/api/login'
import { getUserInfo, getPromotion } from '@/api/user'
export function isLogin(type = false) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('tokens')
    if (!token || type) {
      uni.showModal({
        title: '提示',
        content: '您还未登录，请先登录',
        success: function (res) {
          if (res.confirm) {
            uni.login({
              success: async ({ code }) => {
                const res = await getLogin({ code })
                uni.setStorageSync(
                  'tokens',
                  res.header.Tokens || res.header.tokens
                )
                const { data } = await getUserInfo()
                const superior = uni.getStorageSync('superior')
                if (!data.conten[0].superior && superior) {
                  await getPromotion({ superior })
                  const { data } = await getUserInfo()
                  uni.setStorageSync('userinfo', data.conten[0])
                } else {
                  uni.setStorageSync('userinfo', data.conten[0])
                }
                resolve(true)
              }
            })
          } else if (res.cancel) {
            uni.switchTab({
              url: '/pages/index/index'
            })
            resolve(false)
          }
        }
      })
    }
  })
}
